package com.falcon.sathu.mungmu;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class PageActivity extends BaseActivity {
	/** Called when the activity is first created. */
	private AdView adView;
	private ViewPager myPager;
	private static final String MY_BANNER_UNIT_ID = "a14f3951228e3a7";
	private int currentPos = 1;
	private ImageView volumn;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page);
		volumn = (ImageView) findViewById(R.id.volumn);
		PageViewAdapter adapter = new PageViewAdapter(this);

		myPager = (ViewPager) findViewById(R.id.slide_bar);
		myPager.setAdapter(adapter);
		myPager.setCurrentItem(1);

		myPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				currentPos = arg0;
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

		// Create the adView
		// Lookup R.layout.main
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.ads);

		// Create the adView
		// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
		AdView adView = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);

		// Add the adView to it
		layout.addView(adView);

		// Initiate a generic request to load it with an ad
		AdRequest request = new AdRequest();
		// request.setTesting(true);
		adView.loadAd(request);
	}

	public void next(View view) {
		myPager.setCurrentItem(currentPos + 1);
	}

	public void changeVolumn(View view) {
		if (volumnOn) {
			try {
				stopService(new Intent(this, BackgroundMusic.class));
				volumn.setImageResource(R.drawable.volumn_off);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} else {
			try {
				startService(new Intent(this, BackgroundMusic.class));
				volumn.setImageResource(R.drawable.volumn_on);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		volumnOn = !volumnOn;
	}

	@Override
	public void onDestroy() {
		// adView.destroy();
		super.onDestroy();
	}
}